
/* GCSx
** LIST.H
**
** Listbox, drop-down, etc. dialog widgets
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_LIST_H_
#define __GCSx_LIST_H_

// A single listbox entry
class ListEntry {
public:
    std::string label; // Used to determine equality/collation
    int selected; // Not part of equality
    int disabled; // Not part of equality
    int id; // User-defined- used to determine equality/collation
    int code1; // User-defined- not part of equality
    int code2; // User-defined- not part of equality
    int code3; // User-defined- not part of equality
    
    ListEntry();
    ListEntry(const std::string& eLabel, int eId, int eCode1 = 0, int eCode2 = 0, int eCode3 = 0, int eDisabled = 0);
    ListEntry(const ListEntry& from);

    int operator==(const ListEntry& right) const { return (!myStricmp(label.c_str(), right.label.c_str())) && (id == right.id); }
    int operator!=(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str())) || (id != right.id); }
    int operator<(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str()) < 0) || ((!myStricmp(label.c_str(), right.label.c_str())) && (id < right.id)); }
    int operator>(const ListEntry& right) const { return (myStricmp(label.c_str(), right.label.c_str()) > 0) || ((!myStricmp(label.c_str(), right.label.c_str())) && (id > right.id)); }
    int operator<=(const ListEntry& right) const { return myStricmp(label.c_str(), right.label.c_str()) <= 0; }
    int operator>=(const ListEntry& right) const { return myStricmp(label.c_str(), right.label.c_str()) >= 0; }
    ListEntry& operator=(const ListEntry& right);
};

class WListBox : public Widget {
protected:
    class WidgetScroll* wparent;

    // Height of each value; Width of largest value
    int valueHeight;
    int valueWidth;

    // Values in listbox
    std::vector<ListEntry> contents;
    
    // Number of values
    int numValues;

    // Width in pixels to show
    int showWidth;
    // Number of lines to show
    int showLines;
    
    // Cursor position (focus)
    int cursorPos;
    
    // Currently selected item- -1 for none/-2 for multiple items
    // (used for speed/simplicity)
    int selectedItem;
    enum { SELECTED_NONE = -1, SELECTED_MANY = -2 };
    
    // Select/deselect an item; 'alone' determines if we should attempt to 'add' to selection
    virtual void selectItem(int id, int select = 1, int alone = 1);
    // (can move to outside bounds- position is clipped)
    void moveCursor(int id);

    // Type of selection allowed
    int allowZero;
    int allowMany;
    
    // Current drag action- set or unset?
    int dragSet;
    
    enum {
        // This is the number of X characters to fit in a standard width listbox
        GUI_LISTBOX_DEFAULTWIDTH = 20,
        
        // Default number of lines to show
        GUI_LISTBOX_DEFAULTLINES = 5,
        
        // Padding on left/right of text
        GUI_LISTBOX_HORIZPAD = 3,
    };

public:
    // Width in pixels, lines to show vertically
    // Version that allows exactly one selection, stores id
    // Allows zero selection if lAllowZero true, stores 0
    WListBox(int lId, int* lSelection, int lAllowZero = 0, int lWidth = -1, int lLines = -1);
    ~WListBox();
    
    // Selecting an item selects it solely
    // (doesn't call siblingModified unless selectIt true)
    virtual void addItem(const ListEntry& item, int selectIt = 0);
    // NULL items will not be updated
    void modifyItem(int position, const std::string* newLabel = NULL, const int* newDisabled = NULL, const int* newCode1 = NULL, const int* newCode2 = NULL, const int* newCode3 = NULL);
    void clear();
    void sortItems(); // Case insensitive

    void addTo(Dialog* dialog);

    // Refuse all if empty
    int refuseAll() const;

    virtual int event(int hasFocus, const SDL_Event* event);
    void load();
    void apply();
    virtual void display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset);
    
    // Given an id, returns an item in listbox, NULL if none found
    const ListEntry* findEntry(int id) const;
};

#endif

